<template>
  <div class="note">
    <!-- 笔记图 -->
    <div class="note_img" @click="ck(note)">
      <van-image :src="src" alt="图片详情"></van-image>
    </div>
    <!-- 笔记标题 -->
    <div class="note_title">
      <p>{{ note.title }}</p>
    </div>
    <!-- 笔记所有者 -->
    <div class="note_owner">
      <div class="head">
        <!-- 头像 -->
        <van-image
          width="25"
          height="25"
          round
          :src="note.avatar"
          alt="头像"
        ></van-image>
        <!-- 昵称 -->
        <span class="name">{{ note.name }}</span>
      </div>
      <slot name="icon">
        <div class="like">
          <van-icon
            :name="icon"
            @click="like"
            size="15"
            :color="color"
          ></van-icon>
          <!-- 点赞数 -->
          <span class="num">{{ note.support_num }}</span>
        </div>
      </slot>
    </div>
  </div>
</template>

<script>
import { Image } from "vant";
export default {
  name: "Note",
  props: {
    note: {
      type: Object,
      default() {
        return {
          url: ["2.jpg"],
          title: "这是一个测试标题",
          avatar: "avatar.jpg",
          name: "小武",
          support_num: 9999,
        };
      },
    },
    fla: {
      type: Boolean,
      default: false,
    },
    num :{
      type : Number,
      default : 0
    }
  },
  components: {
    [Image.name]: Image,
  },
  data() {
    return {
      icon: "like-o",
      color: "black",
      flag: true,
      src: "",
    };
  },
  mounted() {
    if (this.fla) {
      this.flag = !this.flag;
      this.icon = "like";
      this.color = "red";
    }
    this.src = this.note.url[0];
  },
  methods: {
    like() {
      this.flag = !this.flag;
      if (this.flag) {
        this.icon = "like-o";
        this.color = "black";
      } else {
        this.icon = "like";
        this.color = "red";
      }
    },
    ck(item) {
      // 评论
      // console.log(item.id);
      this.axios({
        url: "http://127.0.0.1:8081/com",
        method: "post",
        data: {id:item.id },
      })
        .then((res) => {
          let data = {
            item,data:res.data.data,num : this.num
          }
          // console.log(data);
          data =JSON.stringify(data)
          this.$router.push({ name: "Detail", params: {msg:data},});
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>

<style scoped lang="scss">
.note {
  width: 48vw;
  padding: 2%;
  margin-bottom: 2%;
  background: rgb(255, 255, 255);
  border: 1px solid #eee;
  font-size: 12px;

  // note 标题
  .note_title {
    margin: 1%;

    p {
      font-weight: bold;
    }
  }

  // note 所有人
  .note_owner {
    width: 100%;
    display: flex;
    justify-content: space-between;

    .head {
      display: flex;
      align-items: center;

      .name {
        margin-left: 5px;
      }
    }

    .like {
      .num {
        margin-left: 5px;
        font-size: 15px;
      }
    }
  }
}
</style>